﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CvModel;
using System.Data.Entity.Migrations;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class AdminDB
    {
        private static CvModelContainer _context = new CvModelContainer();

        //CREATE
        public static int CreateAdmin(Admin admin)
        {
            try
            {
                var query = _context.Admins.Add(admin);
                return _context.SaveChanges();
            }
            catch (Exception)
        	{
		
		        throw;
	        }
            
        }
        //READ
        public static Admin GetAdmin(string userName, string password)
        {
            try
            {
                var query =
                    (from a in _context.Admins
                     where a.UserName == userName && a.Password == password
                     select new Admin { UserName = a.UserName, AdminTitle = a.AdminTitle, Email = a.Email, CompanyName = a.CompanyName, Phone = a.Phone, Address = a.Address, Zip = a.Zip, City = a.City, IsDeleted = a.IsDeleted }).ToList().First();

                return query;
            }
            catch (Exception)
	        {
		
		        throw;
	        }
        }

        public static bool AdminIsLoggedIn(string userName, string password)
        {
            try
            {
                var query = _context.Admins.Where(a => a.UserName == userName && a.Password == password).ToList().First();

                return query != null;
            }
            catch (Exception)
            {
                throw;
            }
        }
        //UPDATE
        public static int UpdateAdmin(Admin admin)
        {
            try
            {
                _context.Admins.AddOrUpdate(admin);
                return _context.SaveChanges();
            }
            catch (Exception)
	        {
		
		        throw;
        	}
        }
        //DELETE
        public static int DeleteAdmin(int adminId)
        {
            try
            {
                var query =
                    (from a in _context.Admins
                     where a.AdminId == adminId
                     select a).Single();

                _context.Admins.Remove(query);

                return _context.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }


    }
}
